import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.awt.geom.Point2D;
import java.awt.AlphaComposite;
import java.awt.RadialGradientPaint;


public class Main extends JPanel implements Runnable {
    // 游戏状态常量
    private static final int MENU_STATE = 0;
    private static final int INSTRUCTIONS_STATE = 1;
    private static final int GAME_STATE = 2;
    private static final int PAUSE_STATE = 3;
    private static final int GAME_OVER_STATE = 4;
    private static final int GAME_SUCCESS_STATE = 5;

    private int gameState = MENU_STATE;

    // 游戏窗口设置
    protected static final int HEIGHT = 600;
    protected static final int WIDTH = 800;
    protected static final Color BG_COLOR = new Color(240, 255, 255);

    // 游戏实体
    private Embryo embryo;
    private List<GameElement> elements;
    private Random random;
    private List<EvolutionEffect> evolutionEffects = new ArrayList<>();

    // 游戏控制
    private Thread gameThread;
    private boolean running;
    private StageNotifier notifier = new StageNotifier();
    private boolean[] directions = new boolean[4];
    private boolean gamePaused = false;

    // 工厂模式togglePause()
    private ElementSpawner elementSpawner;

    // 菜单按钮
    private JButton startButton;
    private JButton instructionsButton;
    private JButton backButton;
    private JPanel menuPanel;
    private JPanel instructionsPanel;

    // 暂停菜单按钮
    private JButton continueButton;
    private JButton restartButton;
    private JButton menuButton;
    private JPanel pausePanel;

    // 游戏结束按钮
    private JButton gameOverRestartButton;
    private JButton gameOverMenuButton;
    private JPanel gameOverPanel;

    // 游戏成功按钮
    private JButton successRestartButton;
    private JButton successMenuButton;
    private JPanel successPanel;


    private final float[][] displacement = new float[COLS][ROWS];
    private static final int GRID_SIZE = 5; // 波浪网格大小
    private static final int COLS = WIDTH / GRID_SIZE;
    private static final int ROWS = HEIGHT / GRID_SIZE;
    private final float[][] waveHeight = new float[COLS][ROWS]; // 波浪高度
    private final float[][] waveVelocity = new float[COLS][ROWS]; // 波浪速度
    private static final float WAVE_DAMPING = 0.98f; // 波浪阻尼
    private static final float WAVE_SPREAD = 0.5f; // 波浪传播速度


    private static final int ATTACK_RING_RADIUS = 80; // 不可见攻击环半径
    private float attackAngle = 0; // 当前瞄准角度
    private final Point2D.Float attackAnchor = new Point2D.Float(); // 瞄准锚点位置
    private final List<Antibody> antibodies = new ArrayList<>(); // 抗体列表
    private static final float ATTACK_COOLDOWN = 0.3f; // 攻击冷却
    private float attackCooldown = 0; // 当前冷却时间
    private final List<ImmuneEffect> immuneEffects = new ArrayList<>();// 0.3秒冷却
    private final List<HealingParticle> healingParticles = new ArrayList<>();

    // 伤害
    private float shakeIntensity = 0;
    private float hurtEffectAlpha = 0;
    private static final float HURT_FADE_SPEED = 0.05f;
    private Color hurtEffectColor = Color.RED;

    //进化
    private float evolveEffectAlpha = 0;
    private Color evolveEffectColor = new Color(100, 255, 100, 0); // 柔和的绿色


    //攻击冷却
    private static final int COOLDOWN_BAR_WIDTH = 120;
    private static final int COOLDOWN_BAR_HEIGHT = 15;
    private static final int COOLDOWN_BAR_MARGIN = 20;

    public Main() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(BG_COLOR);
        setLayout(new BorderLayout());

        elementSpawner = new ElementSpawner();
        elementSpawner.addFactory(new ProteinFactory());
        elementSpawner.addFactory(new FolicAcidFactory());
        elementSpawner.addFactory(new IronFactory());
        elementSpawner.addFactory(new HarmfulFactory());
        elementSpawner.addFactory(new RadiationFactory());
        elementSpawner.addFactory(new ToxinFactory());

        // 鼠标交互
        MouseHandler handler = new MouseHandler();
        addMouseListener(handler);
        addMouseMotionListener(handler);

        // 初始化游戏元素
        initGameElements();

        // 初始化菜单界面
        initMenu();

        // 初始化说明界面
        initInstructionsPanel();

        // 初始化暂停菜单
        initPausePanel();

        // 初始化游戏结束面板
        initGameOverPanel();

        // 初始化游戏成功面板
        initSuccessPanel();

        // 初始化观察者
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (gameState == GAME_STATE || gameState == PAUSE_STATE) {
                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                        togglePause();  // ESC键
                    } else if (gameState == GAME_STATE) {
                        switch(e.getKeyCode()) {
                            case KeyEvent.VK_W: directions[0] = true; break;
                            case KeyEvent.VK_S: directions[1] = true; break;
                            case KeyEvent.VK_A: directions[2] = true; break;
                            case KeyEvent.VK_D: directions[3] = true; break;
                        }
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (gameState == GAME_STATE) {
                    switch(e.getKeyCode()) {
                        case KeyEvent.VK_W: directions[0] = false; break;
                        case KeyEvent.VK_S: directions[1] = false; break;
                        case KeyEvent.VK_A: directions[2] = false; break;
                        case KeyEvent.VK_D: directions[3] = false; break;
                    }
                }
            }
        });
        setFocusable(true);

    }

    private void initGame() {
        elements = new ArrayList<>();
        random = new Random();
        elementSpawner = new ElementSpawner();
        elementSpawner.addFactory(new ProteinFactory());
        elementSpawner.addFactory(new FolicAcidFactory());
        elementSpawner.addFactory(new IronFactory());
        elementSpawner.addFactory(new HarmfulFactory());
        elementSpawner.addFactory(new RadiationFactory());
        elementSpawner.addFactory(new ToxinFactory());

        GameState.getInstance().reset();
        embryo = GameState.getInstance().getEmbryo();

    }


    private void initGameElements() {
        elements = new ArrayList<>();
        random = new Random();
        elementSpawner = new ElementSpawner();
        elementSpawner.addFactory(new ProteinFactory());
        elementSpawner.addFactory(new FolicAcidFactory());
        elementSpawner.addFactory(new IronFactory());
        elementSpawner.addFactory(new HarmfulFactory());
        elementSpawner.addFactory(new RadiationFactory());
        elementSpawner.addFactory(new ToxinFactory());

        // 初始化胚胎
        GameState.getInstance().reset();
        embryo = GameState.getInstance().getEmbryo();
    }

    private void togglePause() {
        gamePaused = !gamePaused;

        if (gamePaused) {
            gameState = PAUSE_STATE;
            add(pausePanel, BorderLayout.CENTER);
        } else {
            gameState = GAME_STATE;
            remove(pausePanel);
        }
        revalidate();
        repaint();
        requestFocus();
    }



    private void initPausePanel() {
        pausePanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;

                // 半透明暗色覆盖层
                g2d.setColor(new Color(0, 0, 0, 180));
                g2d.fillRect(0, 0, getWidth(), getHeight());

                // 绘制暂停文字
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));

                // 柔和的蓝色文字
                GradientPaint textGradient = new GradientPaint(
                        0, 0, new Color(150, 220, 255),
                        WIDTH, 0, new Color(100, 180, 255)
                );
                g2d.setPaint(textGradient);

                String pauseText = "游戏暂停";
                FontMetrics fm = g2d.getFontMetrics();
                int textWidth = fm.stringWidth(pauseText);
                g2d.drawString(pauseText, 305, 150);
            }
        };
        pausePanel.setLayout(new GridBagLayout());
        pausePanel.setOpaque(false);

        // 按钮样式
        Color buttonColor = new Color(70, 130, 180, 200);
        Color hoverColor = new Color(100, 149, 237, 220);

        // 继续按钮
        continueButton = createStyledButton("继续游戏", buttonColor, hoverColor);
        continueButton.addActionListener(e -> togglePause());

        // 重新开始按钮
        restartButton = createStyledButton("重新开始", buttonColor, hoverColor);
        restartButton.addActionListener(e -> restartGame());

        // 返回菜单按钮
        menuButton = createStyledButton("返回菜单", buttonColor, hoverColor);
        menuButton.addActionListener(e -> returnToMenu());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 100, 10, 100);

        pausePanel.add(continueButton, gbc);
        pausePanel.add(restartButton, gbc);
        pausePanel.add(menuButton, gbc);
    }

    private void initGameOverPanel() {
        gameOverPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;

                // 半透明黑色背景
                g2d.setColor(new Color(0, 0, 0, 180));
                g2d.fillRect(0, 0, getWidth(), getHeight());

                // 绘制游戏结束文字
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));
                g2d.setColor(Color.RED);

                String gameOverText = "游戏结束";
                FontMetrics fm = g2d.getFontMetrics();
                int textWidth = fm.stringWidth(gameOverText);
                g2d.drawString(gameOverText, (WIDTH - textWidth) / 2, 150);
            }
        };
        gameOverPanel.setLayout(new GridBagLayout());
        gameOverPanel.setOpaque(false);

        // 按钮样式
        Color buttonColor = new Color(70, 130, 180, 200);
        Color hoverColor = new Color(100, 149, 237, 220);

        // 重新开始按钮
        gameOverRestartButton = createStyledButton("重新开始", buttonColor, hoverColor);
        gameOverRestartButton.addActionListener(e -> restartGame());

        // 返回菜单按钮
        gameOverMenuButton = createStyledButton("返回菜单", buttonColor, hoverColor);
        gameOverMenuButton.addActionListener(e -> returnToMenu());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 100, 10, 100);

        gameOverPanel.add(gameOverRestartButton, gbc);
        gameOverPanel.add(gameOverMenuButton, gbc);
    }

    private void initSuccessPanel() {
        successPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;

                // 半透明黑色背景
                g2d.setColor(new Color(0, 0, 0, 180));
                g2d.fillRect(0, 0, getWidth(), getHeight());

                // 绘制成功文字
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));
                g2d.setColor(new Color(50, 205, 50));

                String successText = "胚胎成熟!";
                FontMetrics fm = g2d.getFontMetrics();
                int textWidth = fm.stringWidth(successText);
                g2d.drawString(successText, (WIDTH - textWidth) / 2, 150);
            }
        };
        successPanel.setLayout(new GridBagLayout());
        successPanel.setOpaque(false);

        // 按钮样式
        Color buttonColor = new Color(70, 130, 180, 200);
        Color hoverColor = new Color(100, 149, 237, 220);

        // 重新开始按钮
        successRestartButton = createStyledButton("重新开始", buttonColor, hoverColor);
        successRestartButton.addActionListener(e -> restartGame());

        // 返回菜单按钮
        successMenuButton = createStyledButton("返回菜单", buttonColor, hoverColor);
        successMenuButton.addActionListener(e -> returnToMenu());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 100, 10, 100);

        successPanel.add(successRestartButton, gbc);
        successPanel.add(successMenuButton, gbc);
    }

    private void restartGame() {
        // 停止当前游戏线程
        running = false;
        if (gameThread != null) {
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            gameThread = null;
        }

        // 重置游戏状态
        remove(pausePanel);
        remove(gameOverPanel);
        remove(successPanel);

        initGame(); // 重新初始化游戏

        // 开始新游戏
        gameState = GAME_STATE;
        gamePaused = false;
        startGame();
        requestFocus();
    }

    private void returnToMenu() {
        // 停止当前游戏线程
        running = false;
        if (gameThread != null) {
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            gameThread = null;
        }


        // 重置游戏状态
        remove(pausePanel);
        remove(gameOverPanel);
        remove(successPanel);

        // 返回菜单
        gameState = MENU_STATE;
        add(menuPanel, BorderLayout.CENTER);
        revalidate();
        repaint();
    }



    private void initMenu() {
        menuPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;

                // 绘制菜单背景
                drawWavyBackground(g2d);

                // 绘制标题
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));

                // 标题渐变效果
                GradientPaint gradient = new GradientPaint(0, 0, new Color(255, 105, 180),
                        WIDTH, 0, new Color(100, 149, 237));
                g2d.setPaint(gradient);

                String title = "Birth";
                FontMetrics fm = g2d.getFontMetrics();
                int titleWidth = fm.stringWidth(title);
                g2d.drawString(title, 335,150);

                // 副标题
                g2d.setFont(new Font("微软雅黑", Font.PLAIN, 18));
                g2d.setColor(Color.WHITE);
                String subtitle = "培育你的胚胎，从受精卵到成熟";
                int subtitleWidth = fm.stringWidth(subtitle);
                g2d.drawString(subtitle, 270, 190);
            }
        };
        menuPanel.setLayout(new GridBagLayout());
        menuPanel.setOpaque(false);

        // 按钮样式
        Color buttonColor = new Color(70, 130, 180, 200);
        Color hoverColor = new Color(100, 149, 237, 220);

        // 开始按钮
        startButton = createStyledButton("开始游戏", buttonColor, hoverColor);
        startButton.addActionListener(e -> {
            initGame(); // 首次点击时初始化游戏
            gameState = GAME_STATE;
            remove(menuPanel);
            remove(instructionsPanel);
            startGame();
            requestFocus();
        });

        // 说明按钮
        instructionsButton = createStyledButton("物质说明", buttonColor, hoverColor);
        instructionsButton.addActionListener(e -> {
            gameState = INSTRUCTIONS_STATE;
            remove(menuPanel);
            add(instructionsPanel, BorderLayout.CENTER);
            revalidate();
            repaint();
        });

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 100, 10, 100);

        menuPanel.add(startButton, gbc);
        menuPanel.add(instructionsButton, gbc);

        add(menuPanel, BorderLayout.CENTER);
    }

    private void initInstructionsPanel() {
        instructionsPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                drawWavyBackground(g2d);
            }
        };
        instructionsPanel.setLayout(new BorderLayout());
        instructionsPanel.setOpaque(false);

        // 创建内容面板run
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setOpaque(false);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));

        // 标题
        JLabel titleLabel = new JLabel("物质说明");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 32));
        titleLabel.setForeground(new Color(70, 130, 180));
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        contentPanel.add(titleLabel);

        contentPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 物质说明
        addMaterialInfo(contentPanel, "蛋白质", new Color(0, 100, 0),
                "绿色圆形物质\n" +
                        "作用：增加分化值\n" +
                        "特性：叶酸不足时效果减半");

        addMaterialInfo(contentPanel, "叶酸", new Color(255, 215, 0),
                "黄色圆形物质\n" +
                        "作用：维持胚胎健康\n" +
                        "特性：含量低时会导致蛋白质吸收效率降低");

        addMaterialInfo(contentPanel, "铁", new Color(139, 69, 19),
                "棕色圆形物质\n" +
                        "作用：维持胚胎运动能力\n" +
                        "特性：含量低时会减慢移动速度");

        addMaterialInfo(contentPanel, "有害物质", Color.RED,
                "红色圆形物质\n" +
                        "作用：减少健康值\n" +
                        "特性：无特性");

        addMaterialInfo(contentPanel, "辐射", new Color(255, 100, 0),
                "橙色光环物质\n" +
                        "作用：随机影响一项属性\n" +
                        "特性：影响效果是大幅降低(＾Ｕ＾)ノ~");

        addMaterialInfo(contentPanel, "胎毒", new Color(200, 0, 0),
                "红色带黑斑物质\n" +
                        "作用：造成减速和伤害\n" +
                        "特性：接触后会暂时降低移动速度");

        contentPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 胚胎属性说明
        JLabel attrTitle = new JLabel("胚胎属性说明");
        attrTitle.setFont(new Font("微软雅黑", Font.BOLD, 24));
        attrTitle.setForeground(new Color(70, 130, 180));
        attrTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
        contentPanel.add(attrTitle);

        contentPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        addAttributeInfo(contentPanel, "健康值",
                "代表胚胎的存活能力\n" +
                        "降至0时游戏结束\n" +
                        "受有害物质、辐射和胎毒影响");

        addAttributeInfo(contentPanel, "分化值",
                "代表胚胎的发育进度\n" +
                        "达到100%时胚胎成熟\n" +
                        "主要通过吸收蛋白质增加");

        addAttributeInfo(contentPanel, "叶酸量",
                "影响蛋白质的吸收效率\n" +
                        "会随时间缓慢减少\n" +
                        "通过吸收叶酸补充");

        addAttributeInfo(contentPanel, "含铁量",
                "影响胚胎的移动速度\n" +
                        "会随时间缓慢减少\n" +
                        "通过吸收铁补充");

        addAttributeInfo(contentPanel, "抗体",
                "鼠标左键可以发射抗体\n" +
                        "抗体可以消灭坏家伙！\n" +
                        "不知道写什么了但是我们喜欢你");

        // 滚动面板
        JScrollPane scrollPane = new JScrollPane(contentPanel);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());

        instructionsPanel.add(scrollPane, BorderLayout.CENTER);

        // 返回按钮
        backButton = createStyledButton("返回菜单", new Color(70, 130, 180, 200),
                new Color(100, 149, 237, 220));
        backButton.addActionListener(e -> {
            gameState = MENU_STATE;
            remove(instructionsPanel);
            add(menuPanel, BorderLayout.CENTER);
            revalidate();
            repaint();
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.add(backButton);
        instructionsPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addMaterialInfo(JPanel parent, String name, Color color, String description) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setOpaque(false);
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));

        // 物质颜色标识
        JLabel colorLabel = new JLabel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(color);
                g2d.fillOval(0, 0, 30, 30);
            }
        };
        colorLabel.setPreferredSize(new Dimension(30, 30));

        // 物质名称和描述
        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        textPanel.setOpaque(false);

        JLabel nameLabel = new JLabel(name);
        nameLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        nameLabel.setForeground(color.darker());

        JTextArea descArea = new JTextArea(description);
        descArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        descArea.setForeground(Color.WHITE);
        descArea.setOpaque(false);
        descArea.setEditable(false);
        descArea.setLineWrap(true);
        descArea.setWrapStyleWord(true);
        descArea.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));

        textPanel.add(nameLabel);
        textPanel.add(descArea);

        panel.add(colorLabel);
        panel.add(Box.createRigidArea(new Dimension(10, 0)));
        panel.add(textPanel);

        parent.add(panel);
        parent.add(Box.createRigidArea(new Dimension(0, 10)));
    }

    private void addAttributeInfo(JPanel parent, String name, String description) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(false);
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setBorder(BorderFactory.createEmptyBorder(5, 30, 5, 30));

        JLabel nameLabel = new JLabel(name);
        nameLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        nameLabel.setForeground(new Color(70, 130, 180));

        JTextArea descArea = new JTextArea(description);
        descArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        descArea.setForeground(Color.WHITE);
        descArea.setOpaque(false);
        descArea.setEditable(false);
        descArea.setLineWrap(true);
        descArea.setWrapStyleWord(true);
        descArea.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));

        panel.add(nameLabel);
        panel.add(descArea);

        parent.add(panel);
        parent.add(Box.createRigidArea(new Dimension(0, 10)));
    }

    private JButton createStyledButton(String text, Color normalColor, Color hoverColor) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                Color color = getModel().isRollover() ? hoverColor : normalColor;
                g2d.setColor(color);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);

                g2d.setColor(Color.WHITE);
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 18));
                FontMetrics fm = g2d.getFontMetrics();
                int textWidth = fm.stringWidth(getText());
                int textHeight = fm.getHeight();
                g2d.drawString(getText(), (getWidth() - textWidth) / 2,
                        (getHeight() + textHeight) / 2 - fm.getDescent());
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 50);
            }
        };

        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setOpaque(false);

        return button;
    }

    public void startGame() {
        if (gameThread == null) {
            gameThread = new Thread(this);
            running = true;
            gameThread.start();
        }
    }

    @Override
    public void run() {
        long lastTime = System.nanoTime();
        final double NS_PER_UPDATE = 1000000000.0 / 60.0;
        double delta = 0;

        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / NS_PER_UPDATE;
            lastTime = now;

            while (delta >= 1) {
                if (!gamePaused) {  // drawHurtEffect
                    updateGame();
                }
                delta--;
            }

            repaint();

            // 保证循环不会太快
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



    private void updateGame() {

        evolutionEffects.removeIf(e -> !e.update());

        if (random.nextFloat() < 0.03) { // 整体生成概率
            GameElement element = elementSpawner.spawnElement(
                    random.nextInt(WIDTH), random.nextInt(HEIGHT));
            if (element != null) {
                elements.add(element);
            }
        }


        // 更新所有元素
        embryo.update(directions);
        elements.removeIf(e -> !e.update());
        antibodies.removeIf(ab -> !ab.update());
        healingParticles.removeIf(p -> !p.update());

        if(attackCooldown > 0) attackCooldown -= 1/60f;
        // 抗体与有害物碰撞检测
        Iterator<GameElement> elemIt = elements.iterator();
        while(elemIt.hasNext()) {
            GameElement elem = elemIt.next();
            if(elem instanceof Harmful) {
                Iterator<Antibody> abIt = antibodies.iterator();
                while(abIt.hasNext()) {
                    Antibody ab = abIt.next();
                    float dx = ab.x - elem.x;
                    float dy = ab.y - elem.y;
                    float distSq = dx*dx + dy*dy;
                    float minDist = ((float) ab.size /2 + (float) elem.size /2);

                    if(distSq < minDist*minDist) {
                        // 碰撞效果：产生小型爆发
                        createImmuneEffect(ab.x, ab.y);
                        abIt.remove();
                        elemIt.remove();
                        break;
                    }
                }
            }
        }



        // 胚胎移动产生波浪
        if(embryo.getSpeed() > 0.1f) {
            addWaveDisturbance(embryo.getX(), embryo.getY(), embryo.getSpeed()/Embryo.MAX_SPEED*2f);
        }

        // 更新波浪物理
        updateWavePhysics();

        // 碰撞检测
        checkCollisions();
        checkGameOver();
    }

    private void createImmuneEffect(float x, float y) {
        immuneEffects.add(new ImmuneEffect(x, y));
    }


    private void checkCollisions() {
        GameState state = GameState.getInstance();
        Iterator<GameElement> it = elements.iterator();

        while (it.hasNext()) {
            GameElement element = it.next();
            if (embryo.collidesWith(element)) {
                switch (element) {
                    case Protein _ -> {
                        float effect = element.effectValue * embryo.getProteinEffectModifier();
                        state.updateDifferentiation(effect * 1);
                        state.updateHealth(5);
                        createHealingEffect(element.x, element.y);
                    }
                    case FolicAcid _ -> embryo.addFolicAcid(element.effectValue);
                    case Iron _ -> embryo.addIron(element.effectValue);
                    case Radiation _ -> {
                        // 辐射随机影响一项属性
                        int effectType = random.nextInt(3);
                        applyScreenShake(1.5f);
                        hurtEffectAlpha = 0.5f;
                        switch (effectType) {
                            case 0:
                                state.updateDifferentiation(element.effectValue);
                                break;

                            case 1:
                                embryo.addFolicAcid(element.effectValue);
                                break;
                            case 2:
                                embryo.addIron(element.effectValue);
                                break;
                        }
                    }
                    case Toxin toxin -> {
                        toxin.effect(embryo, state);
                        hurtEffectAlpha = 0.7f;
                        applyScreenShake(1.0f);
                    }
                    default -> {
                        state.updateHealth(element.effectValue);
                        state.updateDifferentiation(Math.abs(element.effectValue));
                        applyScreenShake(1.0f);
                        hurtEffectAlpha = 0.5f;
                    }
                }
                it.remove();
            }

            // 修改阶段升级代码
            if (state.shouldUpgradeStage() && state.getCurrentStage() < 4) {
                state.setCurrentStage(state.getCurrentStage() + 1);
                notifier.notifyStageChange(state.getCurrentStage());

                // 进化光环特效
                evolutionEffects.add(new EvolutionEffect(
                        embryo.getX(), embryo.getY(),
                        new Color(100, 255, 100),
                        embryo.getSize() * 3f
                ));

                // 设置进化闪光(绿色)
                evolveEffectAlpha = 0.3f; // 只使用这个新变量
            }
        }

        // 阶段升级检测
        if (state.shouldUpgradeStage() && state.getCurrentStage() < 4) {
            state.setCurrentStage(state.getCurrentStage() + 1);
            notifier.notifyStageChange(state.getCurrentStage());
        }

        Iterator<Antibody> abIt = antibodies.iterator();
        while(abIt.hasNext()) {
            Antibody ab = abIt.next();

            Iterator<GameElement> elemIt = elements.iterator();
            while(elemIt.hasNext()) {
                GameElement elem = elemIt.next();

                if(elem instanceof Harmful || elem instanceof Toxin || elem instanceof Radiation) {
                    float dx = ab.x - elem.x;
                    float dy = ab.y - elem.y;
                    float distSq = dx*dx + dy*dy;
                    float minDist = ((float) ab.size /2 + (float) elem.size /2);

                    if(distSq < minDist*minDist) {
                        createImmuneEffect(ab.x, ab.y);
                        abIt.remove();
                        elemIt.remove();
                        break;
                    }
                }
            }
        }

    }


    private void checkGameOver() {
        GameState state = GameState.getInstance();
        boolean healthDepleted = state.getHealth() <= 0;
        boolean fullyMatured = state.getCurrentStage() == 4
                && state.getDifferentiation() >= 400f;

        if (healthDepleted) {
            running = false;
            JOptionPane.showMessageDialog(this, "Game Over!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        } else if (fullyMatured) {
            running = false;
            showSuccessWindow();
        }
    }

    private void showSuccessWindow() {
        // 成功窗口
        JDialog successDialog = new JDialog((Frame)null, "成功", true);
        successDialog.setLayout(new BorderLayout());
        successDialog.setSize(300, 200);
        successDialog.setLocationRelativeTo(this);

        // 内容
        JLabel label = new JLabel("<html><center><h1>Success!</h1><br>胚胎已成熟!</center></html>",
                SwingConstants.CENTER);
        label.setFont(new Font("微软雅黑", Font.BOLD, 18));
        successDialog.add(label, BorderLayout.CENTER);

        // 确定按钮
        JButton okButton = new JButton("确定");
        okButton.addActionListener(_ -> System.exit(0));
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        successDialog.add(buttonPanel, BorderLayout.SOUTH);

        successDialog.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // 添加背景波动效果
        drawWavyBackground(g2d);

        // 绘制所有游戏元素
        drawGameElements(g2d);
        embryo.draw(g2d);

        // 添加柔光效果
        drawSoftLightEffect(g2d);

        // 绘制优化后的HUD
        drawOptimizedHUD(g2d);

        // 绘制波浪背景
        for(int i=0; i<COLS; i++) {
            for(int j=0; j<ROWS; j++) {
                float h = waveHeight[i][j];
                // 根据波浪高度计算颜色
                Color waveColor = new Color(
                        100,
                        150 + (int)(h*15),
                        255 - (int)(Math.abs(h)*20),
                        50 + (int)(Math.abs(h)*40)
                );

                g2d.setColor(waveColor);
                g2d.fillRect(i*GRID_SIZE, j*GRID_SIZE + (int)(h*3), GRID_SIZE, GRID_SIZE);
            }
        }

        // 绘制所有抗体
        for(Antibody ab : antibodies) {
            ab.draw(g2d);
        }

        drawCooldownBar(g2d);

        g2d.setColor(new Color(255, 255, 0, 200));
        g2d.fillOval(
                (int)attackAnchor.x - 5,
                (int)attackAnchor.y - 5,
                10, 10
        );

        // 黄色光晕效果
        RadialGradientPaint glow = new RadialGradientPaint(
                attackAnchor, 15,
                new float[]{0f, 0.7f, 1f},
                new Color[]{new Color(255, 255, 100, 150),
                        new Color(255, 255, 0, 50),
                        new Color(255, 255, 0, 0)}
        );
        g2d.setPaint(glow);
        g2d.fillOval(
                (int)attackAnchor.x - 15,
                (int)attackAnchor.y - 15,
                30, 30
        );

        // 绘制治疗粒子
        for(HealingParticle p : healingParticles) {
            p.draw(g2d);
        }

        drawEvolveEffect(g2d);//进化特效
        drawOptimizedHUD(g2d);
        drawCooldownBar(g2d);
        drawHurtEffect(g2d);




    }

    private void drawCooldownBar(Graphics2D g2d) {
        int x = COOLDOWN_BAR_MARGIN;
        int y = HEIGHT - COOLDOWN_BAR_HEIGHT - COOLDOWN_BAR_MARGIN;

        // 背景框
        g2d.setColor(new Color(0, 0, 0, 150));
        g2d.fillRoundRect(x, y, COOLDOWN_BAR_WIDTH, COOLDOWN_BAR_HEIGHT, 5, 5);

        // 冷却进度
        if(attackCooldown > 0) {
            float ratio = 1 - (attackCooldown / ATTACK_COOLDOWN);
            int fillWidth = (int)(COOLDOWN_BAR_WIDTH * ratio);

            // 渐变冷却条
            GradientPaint gp = new GradientPaint(
                    x, y, new Color(50, 200, 255),
                    x + fillWidth, y, new Color(100, 255, 200)
            );
            g2d.setPaint(gp);
            g2d.fillRoundRect(x, y, fillWidth, COOLDOWN_BAR_HEIGHT, 5, 5);
        }

        // 边框和文字
        g2d.setColor(Color.WHITE);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawRoundRect(x, y, COOLDOWN_BAR_WIDTH, COOLDOWN_BAR_HEIGHT, 5, 5);

        g2d.setColor(Color.WHITE); // 白色文字
        g2d.drawString("抗体冷却", x+5, y+12);
    }


    private void drawGameElements(Graphics2D g2d) {
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制胚胎
        embryo.draw(g2d);

        // 绘制所有元素
        List<GameElement> sortedElements = new ArrayList<>(elements);
        sortedElements.sort(Comparator.comparingInt(e -> e.size));

        for (GameElement element : sortedElements) {
            // 为每个元素添加轻微投影效果
            drawElementShadow(g2d, element);
            element.draw(g2d);
        }
    }

    private void drawElementShadow(Graphics2D g2d, GameElement element) {
        // 创建元素投影
        g2d.setColor(new Color(0, 0, 0, 50));
        int shadowOffset = (int)(3 * (1 - element.age/element.duration));
        g2d.fillOval(
                (int)element.x - element.size/2 + shadowOffset,
                (int)element.y - element.size/2 + shadowOffset,
                element.size, element.size
        );
        for(EvolutionEffect effect : evolutionEffects) {
            effect.draw(g2d);
        }
    }


    private void drawWavyBackground(Graphics2D g2d) {
        long time = System.currentTimeMillis();

        // 更深的渐变背景以突出水波纹
        Color topColor = new Color(100, 180, 220); // 中等蓝色
        Color bottomColor = new Color(150, 200, 240); // 浅蓝色

        // 背景波浪效果更明显
        float waveOffset1 = (float)Math.sin(time * 0.001) * 30f;
        float waveOffset2 = (float)Math.cos(time * 0.0007) * 25f;

        // 动态渐变背景
        GradientPaint gp = new GradientPaint(
                0, waveOffset1, topColor,
                0, HEIGHT + waveOffset2, bottomColor);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, WIDTH, HEIGHT);

        // 更明显的水波纹效果
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
        for(int i=0; i<10; i++) {  // 增加波纹数量
            float rippleSize = 200 + i*80;  // 增大波纹尺寸
            float rippleX = (float) WIDTH /2 + (float)Math.sin(time*0.0003 + i*0.5) * 100;  // 增大移动幅度
            float rippleY = (float) HEIGHT /2 + (float)Math.cos(time*0.0004 + i*0.5) * 60;

            // 更明显的透明度变化
            float alpha = 0.25f + (float)Math.sin(time*0.0008 + i) * 0.2f;
            g2d.setColor(new Color(180, 220, 255, (int)(alpha*255)));  // 更亮的波纹颜色

            // 使用更平滑的椭圆绘制
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.fillOval(
                    (int)(rippleX - rippleSize/2),
                    (int)(rippleY - rippleSize/2),
                    (int)rippleSize, (int)rippleSize
            );

            // 添加第二层更亮的边缘
            if(i % 2 == 0) {
                g2d.setColor(new Color(220, 240, 255, (int)(alpha*150)));
                g2d.fillOval(
                        (int)(rippleX - rippleSize/2.2),
                        (int)(rippleY - rippleSize/2.2),
                        (int)(rippleSize/1.1), (int)(rippleSize/1.1)
                );
            }
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));

        // 漂浮微粒 - 增加数量和大小
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
        for(int i=0; i<50; i++) {
            float particleSize = 3 + (float)Math.random()*4;
            float particleX = (float)(WIDTH * Math.random());
            float particleY = (float)(HEIGHT * Math.random() ) ;

            // 更明显的波动
            particleX += (float)Math.sin(time*0.0008 + particleY*0.01) * 8;

            g2d.setColor(new Color(255, 255, 255, 150 + (int)(105*Math.random())));
            g2d.fillOval((int)particleX, (int)particleY, (int)particleSize, (int)particleSize);
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
    }

    private void drawSoftLightEffect(Graphics2D g2d) {
        // 更明显的柔光效果
        RadialGradientPaint rgp = new RadialGradientPaint(
                new Point(WIDTH/2, HEIGHT/2),
                HEIGHT*1.2f,
                new float[]{0.1f, 0.8f},
                new Color[]{new Color(255, 200, 220, 100), ColorUtils.TRANSPARENT}
        );

        g2d.setPaint(rgp);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        g2d.fillRect(0, 0, WIDTH, HEIGHT);

        // 添加额外的光晕层
        RadialGradientPaint rgp2 = new RadialGradientPaint(
                new Point(WIDTH/3, HEIGHT/3),
                HEIGHT*0.7f,
                new float[]{0.1f, 0.9f},
                new Color[]{new Color(220, 230, 255, 80), ColorUtils.TRANSPARENT}
        );
        g2d.setPaint(rgp2);
        g2d.fillRect(0, 0, WIDTH, HEIGHT);

        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
    }

    // 创建治疗效果
    private void createHealingEffect(float x, float y) {
        for(int i=0; i<15; i++) {
            healingParticles.add(new HealingParticle(
                    x + (float)(Math.random()*20-10),
                    y + (float)(Math.random()*20-10)
            ));
        }
    }


    private void updateWavePhysics() {
        // 波浪传播计算
        for(int i=1; i<COLS-1; i++) {
            for(int j=1; j<ROWS-1; j++) {
                // 计算周围点的平均高度
                float avg = (waveHeight[i-1][j] + waveHeight[i+1][j] +
                        waveHeight[i][j-1] + waveHeight[i][j+1]) / 4f;
                // 更新速度(基于高度差)
                waveVelocity[i][j] += (avg - waveHeight[i][j]) * WAVE_SPREAD;
                // 应用阻尼
                waveVelocity[i][j] *= WAVE_DAMPING;
            }
        }

        // 更新高度
        for(int i=0; i<COLS; i++) {
            for(int j=0; j<ROWS; j++) {
                waveHeight[i][j] += waveVelocity[i][j];
            }
        }
    }

    // 胚胎移动时产生波浪
    private void addWaveDisturbance(float x, float y, float intensity) {
        int col = (int)(x / GRID_SIZE);
        int row = (int)(y / GRID_SIZE);
        int radius = (int)(intensity * 3); // 影响半径

        for(int i=-radius; i<=radius; i++) {
            for(int j=-radius; j<=radius; j++) {
                if(col+i >=0 && col+i < COLS && row+j >=0 && row+j < ROWS) {
                    float dist = (float)Math.sqrt(i*i + j*j);
                    if(dist <= radius) {
                        // 根据距离计算影响强度
                        float force = (1 - dist/radius) * intensity;
                        waveHeight[col+i][row+j] = force;
                    }
                }
            }
        }
    }

    public void applyScreenShake(float intensity) {
        shakeIntensity = Math.min(shakeIntensity + intensity, 3.0f);  // 限制最大震动强度
    }

    // 绘制红色光圈
    private void drawHurtEffect(Graphics2D g2d) {
        if(hurtEffectAlpha > 0) {
            g2d.setColor(new Color(
                    255, 50, 50,  // 保持红色
                    (int)(hurtEffectAlpha * 255)
            ));
            g2d.fillRect(0, 0, WIDTH, HEIGHT);
            hurtEffectAlpha -= HURT_FADE_SPEED;
        }
    }

    //绿色光圈

    private void drawEvolveEffect(Graphics2D g2d) {
        if(evolveEffectAlpha > 0) {
            g2d.setColor(new Color(
                    evolveEffectColor.getRed(),
                    evolveEffectColor.getGreen(),
                    evolveEffectColor.getBlue(),
                    (int)(evolveEffectAlpha * 255)
            ));
            g2d.fillRect(0, 0, WIDTH, HEIGHT);
            evolveEffectAlpha -= 0.01f;
        }
    }

    private void drawOptimizedHUD(Graphics2D g2d) {
        GameState state = GameState.getInstance();

        // HUD面板设置（右下角竖排）
        int panelWidth = 120;
        int panelHeight = 240;
        int panelX = WIDTH - panelWidth - 20;
        int panelY = HEIGHT - panelHeight - 20;
        int arc = 15;

        // 半透明背景面板
        g2d.setColor(new Color(0, 0, 0, 120));
        g2d.fillRoundRect(panelX, panelY, panelWidth, panelHeight, arc, arc);

        // 边框
        g2d.setColor(new Color(255, 255, 255, 80));
        g2d.setStroke(new BasicStroke(2));
        g2d.drawRoundRect(panelX, panelY, panelWidth, panelHeight, arc, arc);

        // 设置字体
        Font originalFont = g2d.getFont();
        Font boldFont = originalFont.deriveFont(Font.BOLD);

        // 绘制竖排状态条
        int barWidth = 20;
        int barHeight = 80;
        int startX = panelX + 30;
        int startY = panelY + 30;
        int gap = 40;

        // 健康值
        drawVerticalBar(g2d, startX, startY, barWidth, barHeight,
                "健康", state.getHealth()/100f,
                new Color(255, 80, 80), new Color(100, 0, 0));

        // 分化值
        drawVerticalBar(g2d, startX + gap, startY, barWidth, barHeight,
                "分化", state.getDifferentiation()/400f,
                new Color(100, 200, 255), new Color(0, 50, 100));

        // 叶酸
        drawVerticalBar(g2d, startX, startY + barHeight + 35, barWidth, barHeight/2,
                "叶酸", embryo.getFolicAcid()/100f,
                new Color(255, 215, 0), new Color(100, 85, 0));

        // 铁含量
        drawVerticalBar(g2d, startX + gap, startY + barHeight + 35, barWidth, barHeight/2,
                "铁", embryo.getIron()/100f,
                new Color(180, 100, 50), new Color(80, 40, 20));

        // 阶段
        g2d.setColor(Color.WHITE);
        g2d.setFont(boldFont.deriveFont(14f));
        g2d.drawString("阶段: " + getStageName(state.getCurrentStage()),
                panelX + 20, panelY + panelHeight - 15);
    }

    private void drawVerticalBar(Graphics2D g2d, int x, int y, int width, int height,
                                 String label, float value, Color fill, Color bg) {
        // 背景条
        g2d.setColor(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(), 150));
        g2d.fillRoundRect(x, y, width, height, 5, 5);

        // 进度条
        int fillHeight = (int)(height * value);
        g2d.setColor(fill);
        g2d.fillRoundRect(x, y + height - fillHeight, width, fillHeight, 5, 5);

        // 标签
        g2d.setColor(Color.WHITE);
        g2d.drawString(label, x + width/2 - 10, y + height + 15);

        // 百分比
        g2d.drawString(String.format("%d%%", (int)(value * 100)), x, y - 5);
    }


    private String getStageName(int stage) {
        String[] names = {"受精卵", "桑椹胚", "囊胚", "原肠胚", "成熟"}; // 同步修改
        return stage < names.length ? names[stage] : "未知阶段";
    }

    public boolean isAiming() {
        // 是否正在瞄准
        return false;
    }

    public boolean isAttacking() {
        //抗体
        return false;
    }

    // 鼠标处理类
    private class MouseHandler extends MouseAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {
            // 计算锚点位置
            float dx = e.getX() - embryo.getX();
            float dy = e.getY() - embryo.getY();

            // 标准化方向向量
            attackAngle = (float)Math.atan2(dy, dx);
            attackAnchor.x = embryo.getX() + (float)Math.cos(attackAngle) * ATTACK_RING_RADIUS;
            attackAnchor.y = embryo.getY() + (float)Math.sin(attackAngle) * ATTACK_RING_RADIUS;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1 && attackCooldown <= 0) {
                attackCooldown = ATTACK_COOLDOWN;

                // 从胚胎边缘发射抗体
                antibodies.add(new Antibody(
                        embryo.getX(), embryo.getY(), // 从胚胎中心发射
                        attackAngle,
                        WIDTH, HEIGHT
                ));
            }
        }
    }

    class EvolutionEffect {
        float x, y;
        float size;
        float alpha = 1.0f;
        Color color;
        float maxSize;

        public EvolutionEffect(float x, float y, Color color, float maxSize) {
            this.x = x;
            this.y = y;
            this.size = 10;
            this.color = color;
            this.maxSize = maxSize;
        }

        public boolean update() {
            size += 2;  // 减慢膨胀速度
            alpha -= 0.01f;  // 减慢消失速度
            return alpha > 0 && size < maxSize;
        }

        public void draw(Graphics2D g) {
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha * 0.3f));  // 降低透明度
            g.setColor(color);
            g.setStroke(new BasicStroke(3));
            g.drawOval((int)(x - size/2), (int)(y - size/2), (int)size, (int)size);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        }
    }





    public static void main(String[] args) {
        JFrame frame = new JFrame("Embryo Evolution");
        Main game = new Main();
        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        game.startGame();
    }
}





